package projects

import (
	"encoding/json"
	"github.com/beego/beego/v2/core/validation"
	"log"
	"strings"
	"xpms/controllers"
	"xpms/cores/utils"
)

//  任务case管理
type TaskCaseController struct {
	controllers.BaseController
}

// @Title Create task
// @Description create task
// @Param	body		body 	projects.AddTaskCaseDto	true		"body for task content"
// @Success 200  {string} 更新成功
// @Failure 403 body is empty
// @router /pkg [post]
func (u *TaskCaseController) CreatePkg() {
	var dto AddTaskCaseDto
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &dto)
	// 开始校验输入值
	valid := validation.Validation{}

	b, err := valid.Valid(&dto)
	valid.Required(dto.Related, "Related").Message("用例包id为空")
	valid.Required(dto.Taskid, "Taskid").Message("任务id为空")
	if err != nil {
		// handle error
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "严重异常" + err.Error()}
		u.ServeJSON()
		return
	}
	if !b {
		var validMsg []string
		// validation does not pass
		for _, err := range valid.Errors {
			validMsg = append(validMsg, err.Message)
			log.Println(err.Key, err.Message)
		}
		if !utils.IsEmpty(validMsg) {
			u.Data["json"] = map[string]interface{}{"code": 0, "message": strings.Join(validMsg, ",")}
			u.ServeJSON()
			return
		}
	}
	err = AddTaskCase(u.UserUserId, u.UserUsername, dto)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "添加成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败" + err.Error()}
	}
	u.ServeJSON()
}

// @Title delete task case pkg
// @Description  delete task case pkg
// @Param	body		body 	projects.DelTaskCaseDto	true		"body for task content"
// @Success 200  {string} 更新成功
// @Failure 403 body is empty
// @router /pkg [delete]
func (u *TaskCaseController) DeletePkg() {
	var dto DelTaskCaseDto
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &dto)
	// 开始校验输入值
	valid := validation.Validation{}

	b, err := valid.Valid(&dto)
	valid.Required(dto.TaskRelated, "TaskRelated").Message("任务关联id为空")
	if err != nil {
		// handle error
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "严重异常" + err.Error()}
		u.ServeJSON()
		return
	}
	if !b {
		var validMsg []string
		// validation does not pass
		for _, err := range valid.Errors {
			validMsg = append(validMsg, err.Message)
			log.Println(err.Key, err.Message)
		}
		if !utils.IsEmpty(validMsg) {
			u.Data["json"] = map[string]interface{}{"code": 0, "message": strings.Join(validMsg, ",")}
			u.ServeJSON()
			return
		}
	}
	err = DelTaskCase(dto)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "添加成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败" + err.Error()}
	}
	u.ServeJSON()
}

// @Title Update Task
// @Description   Update Task
// @Param	body		body 	[]projects.UpdateTaskCaseStepDto	true		"body for Task content"
// @Success 200 {string} 更新成功
// @Failure 403 body is empty
// @router /step [put]
func (u *TaskCaseController) UpdateTaskCaseStep() {
	var dto []UpdateTaskCaseStepDto
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &dto)
	err = UpdateTaskCaseStep(u.UserUserId, u.UserUsername, dto)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "信息修改成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "信息修改失败"}
	}
	u.ServeJSON()
}

// @Title Update Task
// @Description   Update Task
// @Param	body		body 	projects.UpdateTaskCaseDto	true		"body for Task content"
// @Success 200 {string} 更新成功
// @Failure 403 body is empty
// @router / [put]
func (u *TaskCaseController) UpdateTaskCase() {
	var dto UpdateTaskCaseDto
	err := json.Unmarshal(u.Ctx.Input.RequestBody, &dto)
	err = UpdateTaskCase(u.UserUserId, u.UserUsername, dto)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "信息修改成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "信息修改失败"}
	}
	u.ServeJSON()
}

// @Title 分页查询任务数据
// @Description 分页查询任务数据
// @Param	id		path 	string	true		"任务id"
// @Success 200 {object} projects.QueryTaskCasePkgDto   int   int  int
// @Failure 403 任务 not exist
// @router /:id/pkg [get]
func (u *TaskCaseController) PkgBYTaskid() {
	id, err := u.GetInt64(":id")
	if err != nil || id < 0 {
		// handle error
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "未知任务" + err.Error()}
		u.ServeJSON()
		return
	}
	modeData := QueryTaskCasePkgByTaskId(id)
	u.Data["json"] = modeData
	u.ServeJSON()
}

// @Title 分页查询任务数据
// @Description 分页查询任务数据
// @Param	id		path 	string	true		"任务关联id"
// @Success 200 {object} projects.QueryTaskCaseDto   int   int  int
// @Failure 403 任务 not exist
// @router /:id/case [get]
func (u *TaskCaseController) CaseBYTaskRelid() {
	id, err := u.GetInt64(":id")
	if err != nil || id < 0 {
		// handle error
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "未知任务关联" + err.Error()}
		u.ServeJSON()
		return
	}
	modeData := QueryTaskCaseByTaskRelId(id)
	u.Data["json"] = modeData
	u.ServeJSON()
}
